#include<bits/stdc++.h>
#define ll long long
#define L x<<1
#define R x<<1|1
#define mid (l+r>>1)
#define lc L,l,mid
#define rc R,mid+1,r
#define Root 1,1,n
#define OK l>=Ll&&r<=Rr
#define rep(x,y,z) for(int x=(y);x<=(z);x++)
#define per(x,y,z) for(int x=(y);x>=(z);x--)
#define pb push_back
#define ull unsigned long long
#define e(x) for(int i=h[x],y=to[i];i;i=nxt[i],y=to[i])
#define E(x) for(auto y:p[x])
#define Pi pair<int,int>
#define ui unsigned int 
inline int read(){int s=0,w=1;char c=getchar();while(c<48||c>57) {if(c=='-') w=-1;c=getchar();}while(c>=48&&c<=57) s=(s<<1)+(s<<3)+c-48,c=getchar();return s*w;}
inline void pf(ll x){if(x<0) putchar('-'),x=-x;if(x>9)pf(x/10);putchar(x%10+48);}
const int N =1e3+5,M=4e5+5,inf=2147000000;
const double eps=1e-6;
const ll mod=147744151,llf=1e18;
using namespace std;
int n,m,p,a[N][N],q;
int s[N][N],inv[N][N],ans;
inline Pi exgcd(int X,int Y){
	if(!Y)return {1,0};
	Pi now=exgcd(Y,X%Y);
	int y=now.first,x=now.second;
	return {x,y};
}
inline bool isp(int x){
	for(int i=2;i*i<=x;i++)if(x%i==0)return 0;
	return 1;
}
inline int qp(int a,int b){
	if(!b)return 1;
	ll c=qp(a,b>>1);
	c=c*c%p;
	if(b&1)c=c*a%p;
	return c;
}
int main(){
	freopen("tsuki.in","r",stdin);
	freopen("tsuki.out","w",stdout);
	n=read(),m=read(),p=read();
	rep(i,1,n)rep(j,1,m)a[i][j]=read();
	q=read();
	if(n<=200&&m<=200){
		for(int i=1,r,c,x,y;i<=q;i++){
			r=read(),c=read(),x=read(),y=read();
			ll now=1;
			rep(j,r,x)rep(k,c,y)now=now*(ll)a[j][k]%p;
			ans=(ans+(i^now))%mod;
		}
		cout <<ans;
		return 0;
	}
	
		s[0][0]=inv[0][0]=1;
		rep(i,1,n)s[i][0]=inv[i][0]=1;
		rep(i,1,m)s[0][i]=inv[0][i]=1;
		rep(i,1,n)rep(j,1,m){
			s[i][j]=(ll)s[i-1][j]*(ll)s[i][j-1]%p*(ll)inv[i-1][j-1]%p*a[i][j]%p;
			inv[i][j]=qp(s[i][j],p-2);
		}
		for(int i=1,r,c,x,y;i<=q;i++){
			r=read(),c=read(),x=read(),y=read();
			ll now=(ll)s[x][y]*(ll)s[r-1][c-1]%p*(ll)inv[r-1][y]%p*(ll)inv[x][c-1]%p;
			ans=(ans+(i^now))%mod;
		}
		cout <<ans;
	
	return 0;
}
